//package com.lvhx.springboot.rabbitmq;
//
//import com.lvhx.facade.RunJobService;
//import com.lvhx.facade.dto.RunJobRequest;
//import com.lvhx.springboot.entity.BatchMessage;
//import com.lvhx.springboot.entity.TOrder;
//import com.lvhx.springboot.entity.User;
//import com.lvhx.springboot.entity.batch.BatchJobGroupConfig;
//import com.lvhx.springboot.entity.batch.BatchJobGroupConfigExample;
//import com.lvhx.springboot.mapper.batch.BatchJobGroupConfigMapper;
//import com.rabbitmq.client.Channel;
//import org.springframework.amqp.rabbit.annotation.*;
//import org.springframework.amqp.support.AmqpHeaders;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.handler.annotation.Headers;
//import org.springframework.messaging.handler.annotation.Payload;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Map;
//
///**
// * @auther 吕宏旭
// * @date 2019/8/18 15:39
// */
//@Component
//public class OrderReceiver {
//
//    @Autowired
//    private RabbitOrderSender rabbitOrderSender;
//    @Autowired
//    private BatchJobGroupConfigMapper batchJobGroupConfigMapper;
//    @Autowired
//    private RunJobService runJobService;
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "order-queue", durable = "true"),
//            exchange = @Exchange(value = "order-exchange", durable = "true", type = "topic"),
//            key = "order.*"))
//    @RabbitHandler
//    public void onOrderMassage(@Payload TOrder order, @Headers Map<String, Object> headers, Channel channel) throws Exception {
//
//        System.out.println("------------收到消息，开始消费----------------");
//        System.out.println("订单ID:" + order.getId());
//        //ACK 手工签收
//        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
////        channel.basicAck(deliveryTag, false);
//    }
//
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(value = "batch-queue", durable = "true"),
//            exchange = @Exchange(value = "batch-exchange", durable = "true", type = "topic"),
//            key = "batch.*"))
//    @RabbitHandler
//    public void onBatchMassage(@Payload BatchMessage batchMessage, @Headers Map<String, Object> headers, Channel channel) throws Exception {
//
//        System.out.println("------------收到消息，开始消费----------------");
//        System.out.println("运行批量:" + batchMessage.getJobName());
//        RunJobRequest request = new RunJobRequest();
//        request.setJobName(batchMessage.getJobName());
//        runJobService.runJob(request);
//        //ACK 手工签收
//        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
//        //确认消息
//        channel.basicAck(deliveryTag, false);
//
//        BatchJobGroupConfigExample example = new BatchJobGroupConfigExample();
//        example.createCriteria().andJobNameEqualTo(batchMessage.getJobName())
//                .andGroupIdEqualTo(batchMessage.getGroupId());
//        List<BatchJobGroupConfig> list = batchJobGroupConfigMapper.selectByExample(example);
//        example.clear();
//        example.createCriteria().andGroupIdEqualTo(batchMessage.getGroupId()).andExecuteSeqEqualTo(list.get(0).getExecuteSeq()+1);
//        List<BatchJobGroupConfig> nextList = batchJobGroupConfigMapper.selectByExample(example);
//        if (null != nextList && !nextList.isEmpty()){
//            BatchMessage nextBatchMessage = new BatchMessage();
//            nextBatchMessage.setGroupId(batchMessage.getGroupId());
//            nextBatchMessage.setJobName(nextList.get(0).getJobName());
//            rabbitOrderSender.sendBatchMessage(nextBatchMessage);
//        }
//    }
//}
